package com.qingguo.homework

import org.apache.spark.{SparkConf, SparkContext}

object student2 {
  def main(args: Array[String]): Unit = {

    /**
      * 2、统计总分大于年级平均分的学生 [学号，姓名，班级，总分]
      */

    //创建Spark任务
    val conf = new SparkConf()
    conf.setAppName("Student1").setMaster("local")
    val sc = new SparkContext(conf)

    //读文件
    val score = sc.textFile("Spark/data/score.txt")

    /**
      * 计算每个学生总分，取top10
      */

    //取出成绩
    val scoreRDD = score.map(line => {
      val split = line.split(",")
      val id = split(0)
      val score = split(2).toInt
      (id, score)
    })

    //计算每个学生的总分
    val sumScoresRDD = scoreRDD.reduceByKey(_ + _)

    //计算年级平均分
    val scoureavg = sumScoresRDD
      .map(_._2).sum() / sumScoresRDD.count()

    sumScoresRDD
      .filter(_._2 >= scoureavg)
      .foreach(println)


  }

}
